package com.webdev.newtech.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.webdev.newtech.domain.SysUser;
import com.webdev.newtech.mapper.ISysUserMapper;
import com.webdev.newtech.service.ISysUserService;
import com.webdev.newtech.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements ISysUserService {
    @Resource
    ISysUserMapper userMapper;
    @Override
    public int add(SysUser user) {
        user.setPassword(MD5Util.MD5(user.getPassword()));

        return userMapper.insert(user);
    }

    /**
     * 传递的user对象，包含有用户的ID id属性不为空
     * @param user
     * @return
     */
    @Override
    public int update(SysUser user) {

        return userMapper.updateById(user);
    }

    @Override
    public int removeUserById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public SysUser getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 要查询的条件封装在user对象中
     * @param user
     * @return
     */
    @Override
    public List<SysUser> getUserList(SysUser user) {
        //构造查询条件 QueryWrapper
        QueryWrapper wrapper = new QueryWrapper<SysUser>();
        if(user.getUserName()!=null){
            wrapper.like("user_name",user.getUserName());
        }
        if(user.getLoginName() != null){
            wrapper.eq("login_name",user.getLoginName());
        }
        //三个参数 第一个参数是条件（true的时候会执行这个条件查询，否则不会执行） 第二个参数是数据库的字段名 第三个参数是要传递的参数
        wrapper.ge(user.getAge()!=null,"age",user.getAge());
        wrapper.orderByDesc("id");
        return userMapper.selectList(wrapper);
    }
}
